package com.shine.app.ui;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shine.app.AppManager;
import com.shine.app.BaseActivity;
import com.shine.app.R;
import com.shine.app.bean.MyTeeTimesBean;
import com.shine.app.bean.responses.ValidateResponse;
import com.shine.app.commons.UIHelper;
import com.shine.app.inject.InjectView;
import com.shine.app.inject.Injector;

public class ViewMyInfo_TeeTimeCancelDialogActivity extends BaseActivity
		implements OnClickListener {

	@InjectView(R.id.submit)
	private TextView submiTextView;
	@InjectView(R.id.cancel)
	private TextView cancelTextView;
	@InjectView(R.id.message)
	private TextView messageTextView;
	@InjectView(R.id.layout)
	private LinearLayout layout;
	@InjectView(R.id.progressBar)
	private ProgressBar progressBar;
	private MyTeeTimesBean myTeeTimesBean;
	private TeeTimeCancelAsyncTask teeTimeCancelAsyncTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.dialog_cancelteetimes);
		Injector.get(this).inject();
		init();
	}

	private void init() {
		progressBar.setVisibility(View.GONE);
		myTeeTimesBean = (MyTeeTimesBean) getIntent().getSerializableExtra(
				"myTeeTimesBean");
		cancelTextView.setText(R.string.dialog_no);
		submiTextView.setText(R.string.dialog_yes);
		messageTextView.setText(R.string.teetime_canceldialog);
		submiTextView.setOnClickListener(this);
		cancelTextView.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel:
			AppManager.getAppManager().finishActivity();
			break;
		case R.id.submit:
			layout.setVisibility(View.GONE);
			String golferId = appContext.getLoginInfo().getUser().getGolferId();
			teeTimeCancelAsyncTask = new TeeTimeCancelAsyncTask();
			teeTimeCancelAsyncTask.execute(myTeeTimesBean.getId(), golferId);
			break;

		default:
			break;
		}
	}

	public class TeeTimeCancelAsyncTask extends
			AsyncTask<Object, Integer, ValidateResponse> {
		private String reservationid;
		private String golferId;

		@Override
		protected ValidateResponse doInBackground(Object... params) {
			ValidateResponse response = null;
			reservationid = (String) params[0];
			golferId = (String) params[1];
			response = appContext.cancelReservation(reservationid, golferId);
			return response;
		}

		@Override
		protected void onPostExecute(ValidateResponse result) {
			super.onPostExecute(result);
			if (result != null) {
				if (result.isData()) {
					UIHelper.toastDialog(
							ViewMyInfo_TeeTimeCancelDialogActivity.this,
							R.string.msg_reservation_cancel_success,
							R.string.msg_yes);
				} else {
					UIHelper.toastDialogs(
							ViewMyInfo_TeeTimeCancelDialogActivity.this,
							result.getErrorMessage(), R.string.msg_yes);
				}
			} else {
				UIHelper.toastDialog(
						ViewMyInfo_TeeTimeCancelDialogActivity.this,
						R.string.msg_reservation_cancel_failed,
						R.string.msg_yes);
			}
			AppManager.getAppManager().finishActivity();
			progressBar.setVisibility(View.GONE);
			overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onDestroy() {
		if (teeTimeCancelAsyncTask != null
				&& teeTimeCancelAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			teeTimeCancelAsyncTask.cancel(true);
		}
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AppManager.getAppManager().finishActivity();
		}
		return true;
	}
}
